package com.wiki.KBS.Controller;

import com.wiki.KBS.request.DocReq;
import com.wiki.KBS.request.DocSaveReq;
import com.wiki.KBS.response.CommonResp;
import com.wiki.KBS.response.DocQueryResp;
import com.wiki.KBS.response.PageResp;
import com.wiki.KBS.services.DocService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/doc")
public class DocController {

    private static final Logger log = LoggerFactory.getLogger(DocController.class);
    @Autowired
    private DocService docService;

    @RequestMapping("/all")
    public CommonResp docAll(){
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> docs = docService.docAll();
        resp.setMessage("doc all 数据获取成功！");
        resp.setContent(docs);
        return resp;
    }

    @RequestMapping("/list")
    public CommonResp docList(@Valid DocReq req){
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        PageResp<DocQueryResp> docs = docService.docList(req);
        resp.setMessage("doc list数据获取成功！");
        resp.setContent(docs);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody DocSaveReq req){
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{idsStr}")
    public CommonResp delete(@PathVariable String idsStr){
        CommonResp resp = new CommonResp<>();
        // 传参转为 列表字符串类型
        List<String> list = Arrays.asList(idsStr.split(","));

        // 将字符串 转为 long 类型
        List<Long> longList = list.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());
//        log.info("delete------------------- idsStr，list，longList数据获取成功！{} 和 {}和{}",idsStr,list,longList);
        docService.delete(longList);
        return resp;
    }
}
